package com.study.yygh.cmn.controller;

import com.study.yygh.cmn.service.DictService;
import com.study.yygh.common.result.Result;
import com.study.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author tao
 * @create 2021-09-19 21:17
 * @Description
 */
@Api(value = "数据字典接口")
//@CrossOrigin
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    // 根据dictCode获取下级节点
    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("findByDictCode/{dictCode}")
    public Result  findByDictCode(@PathVariable String dictCode){
        List<Dict> list =  dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }

    //根据数据id查询子数据列表
    @ApiOperation(value="根据数据id查询子数据列表")
    @GetMapping("findChildData/{id}")
    public Result findChildData(@PathVariable("id")Long id){
        List<Dict> childData = dictService.findChildData(id);
        return Result.ok(childData);
    }

    // 导出数据字典接口
    @GetMapping("exportDict")
    public void exportDict(HttpServletResponse response){
        dictService.exportDictData(response);
    }

    // 导入数据字典
    @PostMapping("importDict")
    /*element-ui封装的前端的文件名叫file，后端接受的参数也只能叫MultipartFile file，不然就接收不到*/
    public Result importDict(MultipartFile file){
        dictService.importDictData(file);
        return Result.ok();
    }

    // 根据dictCode和value查询
    @GetMapping("getName/{dictCode}/{value}")
    public String getName(@PathVariable  String dictCode,
                          @PathVariable String value){
        return dictService.getDictName(dictCode, value);
    }
    // 根据value查询
    @GetMapping("getName/{value}")
    public  String getName(@PathVariable String value){
        return dictService.getDictName("", value);
    }
}
